package dao.impl;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import dao.NewsDao;

import po.message.News;

public class NewsDaoImpl implements NewsDao {
	private List<News> news;

	public NewsDaoImpl() {
		news = new LinkedList<>();
	}

	@Override
	public boolean insert(News t) {
		if (news.contains(t)) {
			return false;
		}
		return news.add(t);
	}

	@Override
	public boolean update(News t) {
		int index = news.indexOf(t);
		if (index == -1) {
			return false;
		} else {
			news.set(index, t);
			return true;
		}
	}

	@Override
	public boolean delete(int id) {
		Iterator<News> it = news.iterator();
		while (it.hasNext()) {
			News stu = it.next();
			if (stu.getNewsId() == id) {
				it.remove();
				return true;
			}
		}
		return false;
	}

	@Override
	public News selectById(int id) {
		for (News s : news) {
			if (s.getNewsId() == id) {
				return s;
			}
		}
		return null;
	}

	@Override
	public List<News> selectAll() {
		List<News> copy = new LinkedList<>(news);
		return copy;
	}

}
